<?php
    /**    
   * 读取最后成绩
   *@author kinet 380511703@qq.cn
   *@time 2010-5-10~2010-5-15
   *@version 1.1
   */
   
   require_once( 'contest.class.php' ); 
   require_once( 'contest_problem_list.class.php' ); 
   require_once( 'team_problem.class.php' );  
   class contest_getresult{
         private $contest_no;
         private $contest_load;
         private $contest_begintime;
         private $contest_addtime; 
         private $team_list;
         private $problem_list;  
         private $result_list;      
         private $db;
         /**
         * 载入
         * @param 数据库名
         * @param竞赛名
         */
          public function contest_getresult($db,$vname){
                   $this->db=$db;
                   try{
                   $this->contest_load=new contest_load($db,$vname);
                   }
                   catch(Exceptio $e){
                       throw new Exception('没有此竞赛','8');
                   } 
                   $result=$this->contest_load->get_contest();
                   $this->contest_no=$result['contest_no'];
                   $this->contest_begintime=$result['begin_time'];
                   $this->contest_addtime=$result['addtime'];
                   $this->problem_list=$this->contest_load->get_problem();   
                   $this->team_list=$this->contest_load->get_team();
                   $this->init_result(); //将比赛成绩更新；
                    
          } 
          public function init_result(){
             
             $tlen=count($this->team_list);
             $plen=count($this->problem_list);     
             for($i=0;$i<$tlen;$i++){ //遍历每个队
                 $rightnum=0;                                    
                 $timeuse=0; 
                 for($j=0;$j<$plen;$j++){   //遍历每个题
                     $team_problem=new team_problem($this->db);
                     if($team_problem->load($this->team_list[$i],$this->problem_list[$j])){ 
                         if($team_problem->get('stat')==='right'){  //如果这道题 正确    
                             $rightnum+=1;            //正确题目数加一
                             $usetime= strtotime($team_problem->get('lasttime'))-strtotime($this->contest_begintime);//这道题用时
                             $subtimes=$team_problem->get('subtimes');     //提交次数
                             $timeuse+= $usetime/60+ $subtimes*$this->contest_addtime;  //总用时加 
                         }                     
                     }
                 }
              
                 $contestteam=new contest_team($this->db);
                 $contestteam->load($this->contest_no,$this->team_list[$i]);
                 $contestteam->set('rightnum',$rightnum);
                 $contestteam->set('timeuse',$timeuse); 
                 $contestteam->update(); 
             }   
                   
          } 
          public function get_result_list(){
                $this->result_list=array( );
                $sql='select *
                      from contest_team
                      where contest_no='.$this->contest_no.'
                      order by rightnum desc,timeuse asc,team_no desc
                      ';  
                 $res=$this->db->query($sql); 
                 $i=0;
                 if(!$res) {
                     throw new Exception('有误',12);                       
                 }
                 while($rows = $res->fetchRow( MDB2_FETCHMODE_ASSOC )){
                     $this->result_list[$i]['team_no']=$rows['team_no'];
                     $this->result_list[$i]['rightnum']=$rows['rightnum']; 
                     $this->result_list[$i]['timeuse']=$rows['timeuse'];  
                     $i++;
                     
                 }  
              return $this->result_list;
          }
          
                    
                   
   }
 ?>﻿